package com.restaurant.validators;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.restaurant.domain.Menumodifires;
import com.restaurant.domain.Modifiremaster;
import com.restaurant.service.MenumodifiresService;

public class ModifireDeleteFormValidator implements Validator{

	@Autowired
	private MenumodifiresService objMenumodifiresService;
	
	@Override
	public boolean supports(Class<?> arg0) {
		// TODO Auto-generated method stub
		return Menumodifires.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		Modifiremaster modifiremaster = (Modifiremaster) arg0;
		ArrayList<Menumodifires> listMenumodifires = new ArrayList<Menumodifires>();
		try {
			listMenumodifires = (ArrayList<Menumodifires>) objMenumodifiresService.loadByHql("from Menumodifires where modifiremaster.modifireID = " + modifiremaster.getModifireID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(listMenumodifires.size() > 0){
			errors.rejectValue("alreadyExists", "modifier.exists");
		}
		
	}

}
